/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package buttonOptions;

import constants.GameControllerServerConstants;
import Helper.ButtonSizeTextFieldValidationListener;
import Helper.ErrorHandler;
import Helper.NumberTextFieldValidationListener;
import controllerButtons.NormalControllerButton;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.colorchooser.DefaultColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author luc0
 */
public class NormalButtonOptionFrame extends JFrame{

    private NormalControllerButton controllerButton;
    private JButton saveButton;
    private JPanel optionPanel;
    private JTextField nameTextField;
    private JLabel nameLabel;
    private JLabel buttonToPressLabel;
    private JComboBox buttonToPressComboBox;
    private JLabel buttonHeigtLabel;
    private JLabel buttonWidthLabel;
    private JTextField buttonHeigtTextField;
    private JTextField buttonWidthTextField;
    private JColorChooser colorChooser;
    private JLabel primaryButtonLabel;
    private JComboBox primaryButtonComboBox;
    public NormalButtonOptionFrame(MouseEvent evt,JLayeredPane gameControllerPanel){
        super();

        //This
        this.setSize(600,500);
        this.setLocation(evt.getLocationOnScreen().x,evt.getLocationOnScreen().y);


        //Panel
        optionPanel = new JPanel();
        optionPanel.setSize(this.getSize());
        

        //Layout
        optionPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        //Button
        this.controllerButton=  (NormalControllerButton) evt.getSource();
        saveButton = new JButton("Save");
        saveButton.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
               onSaveButtonClicked(e);
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });

        //Color Chooser
        colorChooser = new JColorChooser();
        colorChooser.setPreviewPanel(new JPanel());
        colorChooser.setSelectionModel(new DefaultColorSelectionModel());
        colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                Color newColor = colorChooser.getColor();
                controllerButton.setColor(newColor);
                controllerButton.repaint();
            }
        });

        //Name Label
        nameLabel = new JLabel("Button Name:");

        //NameTextField
        nameTextField = new JTextField(controllerButton.getText());

        //Primary button
        primaryButtonLabel = new JLabel("Primary Button:");

        primaryButtonComboBox = new JComboBox(new String[]{"","shift","ctrl","alt"});




        //Button To Press Label
        buttonToPressLabel = new JLabel("Button:");

        //Button To Press TextField
        buttonToPressComboBox = new JComboBox(GameControllerServerConstants.AVAILABLE_KEYS);
        buttonToPressComboBox.setSelectedItem(controllerButton.getButtonToPress());
      

        //Button Height Label
        buttonHeigtLabel = new JLabel("Height:");

        //Button Height TextField
        buttonHeigtTextField = new JTextField(String.valueOf(controllerButton.getHeight()));
        buttonHeigtTextField.addFocusListener(new ButtonSizeTextFieldValidationListener(this,"height",25,220));

        //Button width Label
        buttonWidthLabel = new JLabel("Width:");

        //Button width TextField
        buttonWidthTextField = new JTextField(String.valueOf(controllerButton.getWidth()));
        buttonWidthTextField.addFocusListener(new ButtonSizeTextFieldValidationListener(this,"width",25,220));

        //Row 1
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        optionPanel.add(nameLabel,c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        optionPanel.add(nameTextField,c);


        //Row 2
         c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        optionPanel.add(primaryButtonLabel,c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        optionPanel.add(primaryButtonComboBox,c);

        //Row 3
         c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        optionPanel.add(buttonToPressLabel,c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        optionPanel.add(buttonToPressComboBox,c);

        //Row 4
         c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        optionPanel.add(buttonHeigtLabel,c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        optionPanel.add(buttonHeigtTextField,c);

        //Row 5
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        optionPanel.add(buttonWidthLabel,c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 4;
        optionPanel.add(buttonWidthTextField,c);

        //Row 6
          c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 5;
        optionPanel.add(colorChooser,c);

        //Row 7
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 6;
        optionPanel.add(saveButton,c);

        
        

        this.add(optionPanel);

        }



    private void onSaveButtonClicked(MouseEvent evt){
            if(!NumberTextFieldValidationListener.isNumberOfTextFieldValid(buttonHeigtTextField.getText(),25,220)){
                ErrorHandler.invalidHeight(this);
                return;
            }else if(!NumberTextFieldValidationListener.isNumberOfTextFieldValid(buttonWidthTextField.getText(),25,220)){
                ErrorHandler.invalidWidth(this);
                return;
            }
            GameControllerServerConstants.isOptionInUse = false;
            controllerButton.setText(nameTextField.getText());//setBackground(Color.red);
            controllerButton.setButtonToPress((String) buttonToPressComboBox.getSelectedItem());
            setSizeOfButton(controllerButton,Integer.parseInt(buttonWidthTextField.getText()),Integer.parseInt(buttonHeigtTextField.getText()));
            controllerButton.repaint();
            dispose();
    }

    private void setSizeOfButton(NormalControllerButton button,int width, int height){

        button.setSize(width,height);
        int lx = button.getLocation().x;
        int ly = button.getLocation().y;

        if(lx+width>= 480){
            int pixelsToBorder = 480-lx-5;

            int pixelsHidden = width - pixelsToBorder;
            lx = lx-pixelsHidden;
        }
        if(ly+height>=320){
            int pixelsToBorder = 320-ly-5;

            int pixelsHidden = height-pixelsToBorder;
            ly = ly-pixelsHidden;
        }
         button.setLocation(lx, ly);
    }

}
